<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','title','image','status','ishot','isrecommend','livetime');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_live_room',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['url']=custom_url('jy_h5live/room//id='.$item['id'],true);
	}
	$return['status']=array('0'=>'关闭','1'=>'显示');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'直播管理'),			
			array('op'=>'edit','title'=>'添加直播'),
		),
		'list'=>array('data'=>'list','tip'=>'【<red>温馨提示</red>】：部分主播流源，可能采集后不能正常播放，主要是因为主播流源拦截了！
		</br>【<red>科普</red>】：只要是H5的，主播都必须利用第三方直播来支持。但如果用小程序直播，就无需第三方，直接自己小程序就能直播！
		</br>【<red>阿里/腾讯直播教程</red>】：1、添加直播域名（主播端域名+客户端域名）；2、地址生成器，生成主播地址+拉流地址；3、主播用主播软件开启直播；4、把拉流地址填入直播间，这是客户端访问直播间就可以看到直播画面了；
		</br>【<red>三方推流端有哪些？</red>】：1、OBS StudioPC端；2、斗鱼直播助手PC端；3、抖音直播伴侣PC端；4、快手直播伴侣PC端；安装这里主播端，复制上面的推流地址，就可以自定义推流了！
		</br>【<red>阿里云推流端有哪些？</red>】：https://www.alibabacloud.com/help/zh/live/use-cases/live-stream-ingest
		</br>【<red>腾讯推流端有哪些？</red>】：https://cloud.tencent.com/document/product/267/32732
		','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'image','title'=>'封面','type'=>'image'),						
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'ishot','title'=>'热门？','type'=>'switch','set'=>'set'),	
			array('name'=>'isrecommend','title'=>'推荐？','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'直播二维码','type'=>'ewm'),	
			array('name'=>'url','title'=>'直播地址','type'=>'copy'),	
			array('name'=>'livetime','title'=>'直播时间','type'=>'time'),				
		),'action'=>array(
			array('type'=>'href','action'=>'jy_h5live/menu/','title'=>'菜单设置'),
		)
		),
		'search'=>array(
			array('name'=>'title','title'=>'名称','width'=>15,'type'=>'string'),
			array('name'=>'status','title'=>'状态','width'=>15,'type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_live_room',array('id'=>$id));
		$item['menu']=json_decode($item['menu'],true);
		$item['couponid']=json_decode($item['couponid'],true);
		$item['goodsid']=json_decode($item['goodsid'],true);
		$item['livetime']=$item['livetime']?date('Y-m-d H:i',$item['livetime']):date('Y-m-d H:i',time());
	}else{
		$item=array('type'=>'0','livept'=>'inke');
		$item['livetime']=date('Y-m-d H:i',time());
	}
    $return['item']=$item;
	$return['status']=array('0'=>'关闭','1'=>'显示');
	$return['is']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'edit',
		'edit_tab'=>true,
		'post'=>'post',
		'desc'=>'直播设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'直播管理'),
			
			array('op'=>'edit','title'=>'添加直播')
		),
		'edits'=>array(
		array(
		'title'=>'直播设置',
		'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'isscreen','title'=>'是否全屏','type'=>'switch'),
			array('name'=>'mid','title'=>'绑定主播','type'=>'choose','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
			array('name'=>'livetime','title'=>'直播时间','type'=>'time'),
			array('name'=>'group_id','title'=>'分类','type'=>'select','options'=>'group'),
			array('name'=>'type','title'=>'直播平台类型','type'=>'radio','options'=>'type'),
			array('name'=>'livept','title'=>'直播平台','type'=>'radio','options'=>'type0','show'=>array('type'=>0)),
			array('name'=>'livept','title'=>'直播平台','type'=>'radio','options'=>'type1','show'=>array('type'=>1)),
			array('name'=>'livept','title'=>'直播平台','type'=>'radio','options'=>'type2','show'=>array('type'=>2)),			
			array('name'=>'roomewm','title'=>'直播间二维码','type'=>'image','show'=>array('type'=>3)),
			array('name'=>'url','title'=>'url地址','type'=>'modal','read'=>true,'btntitle'=>'视频抓取','action'=>'/room/get/','show'=>array('type'=>array(0,1),'livept'=>array('inke','douyu'))),
			array('name'=>'video','title'=>'HLS直播拉流地址','type'=>'string','help'=>'(<red>HLS直播地址优先</red>)；M3U8直播地址为后缀名为http://域名/xxxxx.m3u8结尾的链接，<red>1、视频无法抓取的时候，可以利用网上一些直播源抓取工具，抓取到直播源地址，然后复制到这！</red>'),
			//array('name'=>'pushurl','title'=>'RTMP直播拉流地址','type'=>'string','help'=>'(<red>M3U8直播地址/RTMP拉流直播地址,二选一，M3U8直播地址优先</red>)；RTMP直播地址（拉流地址/播流地址/原画播放地址）为前缀缀名为rtmp<red>1、微信小程序开启直播后，每个直播间都有自己的拉流地址/播流地址/原画播放地址，直接复制过来也行！</red>'),
			array('name'=>'covertype','title'=>'封面类型','type'=>'radio','options'=>'covertype'),
			array('name'=>'image','title'=>'封面','type'=>'image','show'=>array('covertype'=>0),'help'=>'直播开始前将显示您上传的直播间封面，直播开始后，系统将自动抓取直播平台的图片并替换直播间已上传的封面'),
			array('name'=>'image','title'=>'自定义封面','type'=>'image','show'=>array('covertype'=>1),'help'=>'您上传的图片将作为直播封面显示，您可手动上传修改直播间封面'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
			array('name'=>'isrecommend','title'=>'是否推荐','type'=>'switch'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			),
		),
		array(
				'title'=>'红包',
					'list'=>array(
						array('name'=>'islikered','title'=>'点赞红包？','type'=>'switch','help'=>'开启后，客户点赞直播，随机获得余额红包'),
						array('name'=>'allred','title'=>'总金额','type'=>'unitstring','unit'=>'元','help'=>'总红包额度，超过了不再抽取红包'),
						array('name'=>'hb_rate','title'=>'抽中概率','type'=>'unitstring','unit'=>'%','help'=>'整数：0-100'),
						array('name'=>'maxhb','title'=>'最大红包','type'=>'unitstring','unit'=>'元','help'=>'最大抽中红包额度'),
						array('name'=>'minhb','title'=>'最小红包','type'=>'unitstring','unit'=>'元','help'=>'最小抽中红包额度'),
					)
		),
		array(
				'title'=>'商品',
					'list'=>array(
						array('type'=>'array','name'=>'goodsid','add_tip'=>'添加商品',
							'list'=>array(
								array('name'=>'id','title'=>'商品id','type'=>'string'),
								array('name'=>'price','title'=>'直播价格','type'=>'string'),
							),
							'details'=>array(
								array('type'=>'choose','title'=>'选择商品','name'=>'id','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择商品'),
								array('name'=>'price','title'=>'直播价格','type'=>'string','help'=>'直播价只针对单规格商品有效；如果是<red>多规格</red>商品，直播价将失效'),
							)
						),
					)
		),
		array(
				'title'=>'优惠券',
					'list'=>array(
						array('name'=>'iscoupon','title'=>'状态','type'=>'switch'),
						array('type'=>'array','name'=>'couponid','add_tip'=>'添加优惠券',
							'list'=>array(
								array('name'=>'id','title'=>'优惠券id','type'=>'string'),
								array('name'=>'num','title'=>'总数','type'=>'string'),
								array('name'=>'onenum','title'=>'每人限领数','type'=>'string'),
							),
							'details'=>array(
								array('type'=>'choose','title'=>'选择优惠券','name'=>'id','action'=>'jy_weishop/coupon.get_coupon/','choose_title'=>'选择优惠券'),
								array('name'=>'num','title'=>'总数','type'=>'string'),
								array('name'=>'onenum','title'=>'每人限领数','type'=>'string'),
							)
						),
					)
		),
		array(
		'title'=>'分享/公告/邀请卡设置',
		'list'=>array(
			array('name'=>'invitation_id','title'=>'选择邀请卡','type'=>'select','options'=>'invitation','help'=>'安装了《邀请卡》插件才会显示'),
			array('name'=>'notice','title'=>'公告标题','type'=>'string'),
			array('name'=>'notice_url','title'=>'公告链接','type'=>'link','from'=>'action'),
			array('name'=>'share_title','title'=>'分享标题','type'=>'string'),			
			array('name'=>'share_image','title'=>'分享图片','type'=>'string'),
			array('name'=>'share_desc','title'=>'分享描述','type'=>'string'),
			),
		),
	),
	);
	$return['group']=pdo_getall('jy_weishop_live_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$install = check_account_permission('jy_invitation');
	if($install){
		$return['invitation']=pdo_getall('jy_invitation_list',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'));
	}
	$return['covertype']=array('0'=>'自动抓取直播封面','1'=>'自定义封面');
	$return['type']=array('0'=>'第三方平台','1'=>'智能摄像头','2'=>'行业解决方案','3'=>'企业微信');
	$return['type0']=array('inke'=>'映客直播','douyu'=>'斗鱼直播','douyin'=>'抖音直播','huajiao'=>'花椒直播','yizhibo'=>'一直播');		
	$return['type1']=array('qlive'=>'青果直播','ys7'=>'萤石直播');
	$return['type2']=array('tencentcloud'=>'腾讯云直播','alicloud'=>'阿里云直播','other'=>'其他直播');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写标题','error');
	}
	if(!$postdata['livetime']){
		exi('请填写时间','error');
	}
	if(!$postdata['url'] && !$postdata['video'] && !$postdata['pushurl'] && $postdata['type'] !=3){
		exi('请填写url地址或直播地址','error');
	}
	$data=array_fields(array('islikered','hb_rate','allred','minhb','maxhb','mid','image','title','type','livetime','livept','notice','status','share_image','share_desc','share_title','group_id','url','isrecommend','ishot','paixu','video','pushurl','isscreen','goodsid','iscoupon','couponid','invitation_id','covertype','cover','roomewm'),$postdata);
	$data['menu']=ijson_encode($data['menu']);
	$data['livetime']=strtotime($data['livetime']);
	$data['goodsid']=ijson_encode($data['goodsid']);
	$data['couponid']=ijson_encode($data['couponid']);
	$id=intval($postdata['id']);
	if($id){
				$item=pdo_get('jy_weishop_live_room',array('id'=>$id));
				if ($item['livetime'] < $data['livetime']) {
					$data['subscribenotice'] = 0;
				}
				pdo_update('jy_weishop_live_room',$data,array('id'=>$id));
				exi('设置成功！','','list');
	}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$data['pluginname']=$_SESSION['pluginname'];
				$data['createtime']=time();
				pdo_insert('jy_weishop_live_room',$data);
				exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的直播间！','error');
	}
	$post=get_postdata();
	if(isset($post['ishot'])){
		pdo_update('jy_weishop_live_room',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isrecommend'])){
		pdo_update('jy_weishop_live_room',array('isrecommend'=>intval($post['isrecommend'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['status'])){
		pdo_update('jy_weishop_live_room',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_live_room',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_live_room',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='get'){
	if ($_RQ['postdata']){
		$post=get_postdata();
			$url = trim($post['url']);
			$type = trim($post['livept'])?:'auto';
			if (empty($url)) {
				exi('请输入PC端直播地址','error');
			}
			if (!strexists($url, 'http://') && !strexists($url, 'https://')) {
				exi('直播地址请以http://或https://开头','error');
			}
			$result = $this->model('model')->getLiveInfo($url, $type);
			if (is_error($result)) {
				exi($result['message'],'error');
			}		
			exi($result);
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'视频抓取',
		'edit'=>array(
			array('name'=>'url','title'=>'PC端直播地址','type'=>'string','help'=>'注意：请复制直播间详情页的链接 例如：http://www.inke.cn/liveroom/index.html?uid=xxxxx&id=xxxx (以http://或https://开头)</br>[<red>温馨提示</red>]：考虑获取个大直播流的规则变动频繁，经常会采集不到直播源。建议可以网上搜索一些直播源抓取工具。然后把直播源地址复制到下面的：《视频流》输入框！'),
		)
	);
	//$return['htmls']=$htmls;
	$return['livept']=$this->livept;
	exi($return);
}
?>